package com.xxx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Code;
import com.xxx.common.Result;
import com.xxx.pojo.ClassInfo;
import com.xxx.service.ClassInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/classInfo")
public class ClassInfoController {

    @Autowired
    private ClassInfoService classInfoService;


    /**
     * 查找分类类型
     * @param types
     * @return
     */
    @GetMapping("/typesName")
    public Result getClassType(String types){
        List<String> typesName = classInfoService.getTypesName(types);
        Result result = new Result();
        result.setData(typesName);
        return result;
    }

    /**
     * 分页查询
     * @param pageNo 当前页数
     * @param pageSize 当前数据多少
     * @param name
     * @return
     */
    @GetMapping
    public Result getByPage(int pageNo,int pageSize,String name) {

        IPage page = new Page(pageNo,pageSize);
        page = classInfoService.getByPage(page, name);

        Result r = new Result();
        if (page.getRecords() == null){
            r.setCode(Code.GET_ERR);
            r.setMessage("系统错误，请稍后再试！！");
        }else {
            r.setData(page);
            r.setCode(Code.GET_OK);
        }
        return r;
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        int i = classInfoService.deleteById(id);
        Result r = new Result();
        if(i>0){
            r.setCode(Code.DELETE_OK);
            r.setMessage("删除成功~~~");
        }else {
            r.setCode(Code.DELETE_ERR);
            r.setMessage("删除失败，请稍后再试");
        }
        return r;
    }

    @PostMapping
    public Result save(HttpServletRequest request, @RequestBody ClassInfo classInfo){
//        HttpSession session = request.getSession();
//        Long employeeId = (Long) session.getAttribute("employeeId");
//        LocalDateTime dateTime = LocalDateTime.now();
//
//        classInfo.setCreateTime(dateTime);
//        classInfo.setUpdateTime(dateTime);
//        classInfo.setCreateUser(employeeId);
//        classInfo.setUpdateUser(employeeId);

//        System.out.println(classInfo);

        int save = classInfoService.save(classInfo);

        Result r = new Result();
        if (save > 0){
            r.setCode(Code.SAVE_OK);
            r.setMessage("添加成功~~~");
        }else {
            r.setCode(Code.SAVE_ERR);
            if(save == -1){
                r.setMessage("已经存在该数据");
            }
        }

        return r;
    }

    @PutMapping
    public Result update(HttpServletRequest request,@RequestBody ClassInfo classInfo){
//        HttpSession session = request.getSession();
//        Long employeeId = (Long) session.getAttribute("employeeId");
//        LocalDateTime dateTime = LocalDateTime.now();
//
//        classInfo.setUpdateUser(employeeId);
//        classInfo.setUpdateTime(dateTime);
//
//        System.out.println(classInfo);
        int update = classInfoService.update(classInfo);

        Result r = new Result();
        if (update > 0){
            r.setCode(Code.UPDATE_OK);
            r.setMessage("修改成功！！！");
        }else {
            r.setCode(Code.UPDATE_ERR);
            r.setMessage("该分类名称已存在！！！");
        }

        return r;
    }
}
